﻿Maiks_Goodies = LibStub("AceAddon-3.0"):NewAddon("Maiks_Goodies", "AceConsole-3.0", "AceEvent-3.0");

buttoncount = 0

function Maiks_Goodies:OnInitialize()
    -- Called when the addon is loaded
    Maiks_Goodies:RoleScan()
    self.db = LibStub("AceDB-3.0"):New("Maiks_GoodiesDB", defaults, "Default")
    LibStub("AceConfig-3.0"):RegisterOptionsTable("Maiks_Goodies", createoptions())
    self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Maiks_Goodies", "Maiks: Goodies", "Maiks")
    Maiks_Goodies:RegisterEvent("CHAT_MSG_ADDON", "Update");
    Maiks_Goodies:RegisterEvent("CHAT_MSG_SYSTEM", "Update");
	Maiks_Goodies:RegisterEvent("ROLE_POLL_BEGIN", "Update");
    Maiks_Goodies:Defaults()
end

function Maiks_Goodies:Defaults()
	if Maiks_GoodiesDB.Options == nil then Maiks_GoodiesDB.Options = {} end
	if Maiks_GoodiesDB.Options.PvpButton == nil then Maiks_GoodiesDB.Options.PvpButton = false end
	if Maiks_GoodiesDB.Role == nil then Maiks_GoodiesDB.Role = "None" end
	if Maiks_GoodiesDB.RoleDisable == nil then Maiks_GoodiesDB.RoleDisable = false end
end

function Maiks_Goodies:Update(arg1)
	Maiks_Goodies:PvPButton()
	if arg1 == "ROLE_POLL_BEGIN" then Maiks_Goodies:RolecheckInitiated() end
end

function createoptions()

	options = {
		name = "Maiks: Goodies",
		handler = Maiks_Goodies,
		type = "group",
		childGroups = "tab",
		args = {
			MainGroup = {
				name = "Options",
				type = "group",
				args = {
					header1 = {
						name = "Disable Bottons",
						type = "header",
						order = 1,
					},
					PvpButton = {
						name = "PvP Button Disable fuction",
						desc = "Enables / disables the PvP Join button wile in a group.",
						type = "toggle",
						order = 2,
						width = "full",
						set = function(info, arg1) Maiks_GoodiesDB.Options.PvpButton = arg1 return Maiks_GoodiesDB.Options.PvpButton end,
						get = function(info) return Maiks_GoodiesDB.Options.PvpButton end,
					},
					header2 = {
						name = "Disable Role Check",
						type = "header",
						order = 9,
					},
					desc1 = {
						name = "Disable Role Check because i am always:",
						type = "description",
						order = 9.1,
					},
					icon1 = {
						name = "",
						type = "execute",
						image = "Interface/LFGFRAME/UI-LFG-ICON-ROLES",
						imageCoords = {(0/1200),(300/1200),(325/1200),(625/1200)},
						imageWidth = 100,
						imageHeight = 100,
						func = function() Maiks_Goodies:SetRole("Tank", "set") end,
						disabled = function() return Maiks_Goodies:RoleScan("Tank", "disabled") end,
						width = "half",
						order = 10,
					},
					roleButtonTank = {
						name = "Tank",
--						desc = "",
						type = "toggle",
						order = 10.1,
						width = "half",
						set = function() Maiks_Goodies:SetRole("Tank", "set") end,
						get = function() return Maiks_Goodies:SetRole("Tank", "get") end,
						disabled = function() return Maiks_Goodies:RoleScan("Tank", "disabled") end,
					},
					icon2 = {
						name = "",
						type = "execute",
						image = "Interface/LFGFRAME/UI-LFG-ICON-ROLES",
						imageCoords = {(325/1200),(625/1200),(0/1200),(300/1200)},
						imageWidth = 100,
						imageHeight = 100,
						func = function() Maiks_Goodies:SetRole("Healer", "set") end,
						disabled = function() return Maiks_Goodies:RoleScan("Healer", "disabled") end,
						width = "half",
						order = 11,
					},
					roleButtonHealer = {
						name = "Healer",
--						desc = "",
						type = "toggle",
						order = 11.1,
						width = "half",
						set = function() Maiks_Goodies:SetRole("Healer", "set") end,
						get = function() return Maiks_Goodies:SetRole("Healer", "get") end,
						disabled = function() return Maiks_Goodies:RoleScan("Healer", "disabled") end,
					},
					icon3 = {
						name = "",
						type = "execute",
						image = "Interface/LFGFRAME/UI-LFG-ICON-ROLES",
						imageCoords = {(325/1200),(625/1200),(325/1200),(625/1200)},
						imageWidth = 100,
						imageHeight = 100,
						func = function() Maiks_Goodies:SetRole("Damager", "set") end,
						disabled = function() return Maiks_Goodies:RoleScan("Damager", "disabled") end,
						width = "half",
						order = 12,
					},
					roleButtonDamager = {
						name = "Damager",
--						desc = "",
						type = "toggle",
						order = 12.1,
						width = "half",
						set = function() Maiks_Goodies:SetRole("Damager", "set") end,
						get = function() return Maiks_Goodies:SetRole("Damager", "get") end,
						disabled = function() return Maiks_Goodies:RoleScan("Damager", "disabled") end,
					},
					roledisable = {
						name = "Disable Role Check",
--						desc = "",
						type = "toggle",
						order = 12.1,
						width = "full",
						set = function(info, val) Maiks_GoodiesDB.RoleDisable = val end,
						get = function() return Maiks_GoodiesDB.RoleDisable end,
					},
				},
			},      
		},
	}

	return options
end
        
function Maiks_Goodies:PvPButton()

	if Maiks_GoodiesDB.Options.PvpButton and GetNumPartyMembers() >= 1 then
		PVPFrameLeftButton:Disable()
	else
		PVPFrameLeftButton:Enable()
	end 
end

function Maiks_Goodies:RolecheckInitiated()
	if Maiks_GoodiesDB.RoleDisable then
		if Maiks_GoodiesDB.Role == "Tank" then
			RolePollPopupRoleButtonTank:Click()
			RolePollPopupAcceptButton:Click()
			if RolePollPopup:IsShown() then
				RolePollPopupRoleButtonTank:Click()
				RolePollPopupAcceptButton:Click()
			end
		elseif Maiks_GoodiesDB.Role == "Healer" then
			RolePollPopupRoleButtonHealer:Click()
			RolePollPopupAcceptButton:Click()
			if RolePollPopup:IsShown() then
				RolePollPopupRoleButtonHealer:Click()
				RolePollPopupAcceptButton:Click()
			end
		elseif Maiks_GoodiesDB.Role == "Damager" then
			RolePollPopupRoleButtonDPS:Click()
			RolePollPopupAcceptButton:Click()
			if RolePollPopup:IsShown() then
				RolePollPopupRoleButtonDPS:Click()
				RolePollPopupAcceptButton:Click()
			end
		end
	end
end

function Maiks_Goodies:RoleScan(arg1, arg2)
	local canBeTank, canBeHealer, canBeDamager = UnitGetAvailableRoles("player")
	if arg1 == "Tank" then
		if canBeTank == true then 
			if arg2 == "disabled" then
				return false
			elseif arg2 == "setrole" then
				return true
			end
		else
			if arg2 == "disabled" then
				return true
			elseif arg2 == "setrole" then
				return false
			end
		end
	elseif arg1 == "Healer" then
		if canBeHealer == true then  
			if arg2 == "disabled" then
				return false
			elseif arg2 == "setrole" then
				return true
			end
		else
			if arg2 == "disabled" then
				return true
			elseif arg2 == "setrole" then
				return false
			end
		end
	elseif arg1 == "Damager" then
		if canBeDamager == true then  
			if arg2 == "disabled" then
				return false
			elseif arg2 == "setrole" then
				return true
			end
		else
			if arg2 == "disabled" then
				return true
			elseif arg2 == "setrole" then
				return false
			end
		end
	else   
		if arg2 == "disabled" then
			return true
		elseif arg2 == "setrole" then
			return false
		end
	end
end

function Maiks_Goodies:SetRole(arg1, arg2)
	if arg2 == "get" then 
		if arg1 == "Tank" then
			if IsHealer or IsDamager then
				IsTank = false
				return false
			elseif Maiks_Goodies:RoleScan(arg1, "setrole") and Maiks_GoodiesDB.Role == "Tank" then
				IsTank = true
				return true
			else
				IsTank = false
				return false
			end
		elseif arg1 == "Healer" then
			if IsTank or IsDamager then
				IsHealer = false
				return false
			elseif Maiks_Goodies:RoleScan(arg1, "setrole") and Maiks_GoodiesDB.Role == "Healer" then
				IsHealer = true
				return true
			else
				IsHealer = false
				return false
			end
		elseif arg1 == "Damager" then
			if IsHealer or IsTank then
				IsDamager = false
				return false
			elseif Maiks_Goodies:RoleScan(arg1, "setrole") and Maiks_GoodiesDB.Role == "Damager" then
				IsDamager = true
				return true
			else
				IsDamager = false
				return false
			end
		else
			return false
		end
	elseif arg2 == "set" then
		Maiks_GoodiesDB.Role = arg1
	end
end